package com.leo.foodmaster.admin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.SysOperLogEntity;
import com.leo.foodmaster.admin.system.mapper.SysOperLogMapper;
import com.leo.foodmaster.admin.system.service.ISysOperLogService;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-12-22
 */
@Service
@AllArgsConstructor
@EnableAsync
public class ISysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLogEntity> implements ISysOperLogService {

	/**
	 * 异步保存系统操作日志
	 * @param sysOperLogEntity 日志信息
	 */
	@Async
	public void asyncSaveOperLog(SysOperLogEntity sysOperLogEntity) {
		save(sysOperLogEntity);
	}


	@Override
	public IPage<SysOperLogEntity> pageList(Page<SysOperLogEntity> page, String title, String operName, String businessType,
											String status, String beginTime, String endTime) {
		LambdaQueryWrapper<SysOperLogEntity> qw = Wrappers.lambdaQuery(SysOperLogEntity.class)
				.like(StrUtil.isNotBlank(title), SysOperLogEntity::getTitle, title)
				.like(StrUtil.isNotBlank(operName), SysOperLogEntity::getOperName, operName)
				.like(StrUtil.isNotBlank(businessType), SysOperLogEntity::getBusinessType, businessType)
				.like(StrUtil.isNotBlank(status), SysOperLogEntity::getStatus, status)
				.ge(StrUtil.isNotBlank(beginTime), SysOperLogEntity::getOperTime, beginTime)
				.le(StrUtil.isNotBlank(endTime), SysOperLogEntity::getOperTime, endTime);
		return page(page, qw);
	}

	@Override
	public List<SysOperLogEntity> getList(String title, String operName, String businessType, String status, String beginTime, String endTime) {
		LambdaQueryWrapper<SysOperLogEntity> qw = Wrappers.lambdaQuery(SysOperLogEntity.class)
				.like(StrUtil.isNotBlank(title), SysOperLogEntity::getTitle, title)
				.like(StrUtil.isNotBlank(operName), SysOperLogEntity::getOperName, operName)
				.like(StrUtil.isNotBlank(businessType), SysOperLogEntity::getBusinessType, businessType)
				.like(StrUtil.isNotBlank(status), SysOperLogEntity::getStatus, status)
				.ge(StrUtil.isNotBlank(beginTime), SysOperLogEntity::getOperTime, beginTime)
				.le(StrUtil.isNotBlank(endTime), SysOperLogEntity::getOperTime, endTime);
		return list(qw);
	}
}
